//
//  MiddleManInfoEditVc.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/5/18.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "MiddleManInfoEditVc.h"
#import "config.h"
#import "LocPickView.h"
#import "CompanyInfoEditView.h"

#import "StringTool.h"
#import "UserModal.h"


#import "PhootSelecteView.h"
#import "DealerDataManage.h"
#import "VPImageCropperViewController.h"
#import "Tools.h"
#import "UIButton+WebCache.h"


@interface MiddleManInfoEditVc ()<UITextFieldDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,VPImageCropperDelegate>

//真实姓名
@property (weak, nonatomic) IBOutlet UITextField *trueNameEt;

@property (weak, nonatomic) IBOutlet UITextField *companyNameEt;
@property (weak, nonatomic) IBOutlet UITextField *rzEt;

@property (weak, nonatomic) IBOutlet UIButton *headBtn;

@property (weak, nonatomic) IBOutlet UITextField *phoneEt;


@property (nonatomic,strong) LocPickView *locateView;
@property (nonatomic,strong) CompanyInfoEditView *editView;

@property (nonatomic,strong)PhootSelecteView *menuView;
@property DealerDataManage *dm;



@end

@implementation MiddleManInfoEditVc
@synthesize locateView,editView,menuView,dm;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self  initView];
}

-(void)initView
{
    
    
    [self.headBtn setBackgroundColor:NoImgColor];
    
    [self.headBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:self.model.avatar] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"headtop_pic"]];
    
    [self.titleView setMianTitle:@"信息管理"];
    self.titleView.doneBtn.hidden = YES;
    
    [self.companyNameEt setTintColor:[UIColor redColor]];
    [self.rzEt setTintColor:[UIColor redColor]];
    [self.phoneEt setTintColor:[UIColor redColor]];
    [self.trueNameEt setTintColor:[UIColor redColor]];
   
    if (![StringTool isBlankString:self.model.realname]) {
        self.trueNameEt.text = self.model.realname;
    }
    if (![StringTool isBlankString:self.model.company]) {
        self.companyNameEt.text = self.model.company;
    }
    if (self.model.validate == 1)
    {
        self.rzEt.text = @"已认证";
    }else
    {
        self.rzEt.text = @"未认证";
    }
    if (![StringTool isBlankString:self.model.mobile]) {
        self.phoneEt.text = self.model.mobile;
    }

    dm = [DealerDataManage new];
    
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    
    CGRect dd =  [textField convertRect:textField.frame toView:self.view];
    
    int tvbotY = dd.size.height+dd.origin.y;
    int mid  = PMH-[UserModal shareUserModel].keyboardHeight-tvbotY;
    if (mid<0) {
        
        
        const int movementDistance = mid; // tweak as needed
        const float movementDuration = 0.3f; // tweak as needed
        
        int movement = (up ? movementDistance : -movementDistance);
        
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
        
        [UIView commitAnimations];
        
    }
    
}

#pragma et delegare
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [self animateTextField:textField up:false];
    
    [textField resignFirstResponder];
    return  YES;
}

- (IBAction)headBtnOnClick:(id)sender {
    
    if (self.menuView==nil) {
        menuView = [PhootSelecteView creadInstance];
        [menuView.tackPicBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        [menuView.selectePicBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    [menuView showInView:self.view];

}


-(void)btnOnclick:(id)sender{
    
    if (sender==menuView.tackPicBtn) {
        
        // 跳转到相机
        NSUInteger sourceType = UIImagePickerControllerSourceTypeCamera;
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = NO;
        imagePickerController.sourceType = sourceType;
        imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:imagePickerController animated:YES completion:^{}];
        
    }else if(sender==menuView.selectePicBtn){
        
        // 跳转到相册
        NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = NO;
        
        imagePickerController.sourceType = sourceType;
        imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }
}



#pragma mark 上传头像
-(void)upImg:(UIImage*)img{
    
    [menuView hide];
    
    [dm upHeadImg:img View:self.headBtn];
}




#pragma mark  UiPickView delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    image = [Tools fixOrientation:image];
    
    int h = [Tools getHightDifPMsW:100/2 sh:100/2 nw:PMW];
    CGRect buf = CGRectMake(0, PMH/2-h/2,PMW, h);
    
    // 裁剪
    VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewController alloc] initWithImage:image cropFrame:buf limitScaleRatio:6];
    imgEditorVC.delegate = self;
    [self presentViewController:imgEditorVC animated:YES completion:^{}];
    
}



- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}





#pragma mark VPImageCropperDelegate
- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)image {
    
    [cropperViewController dismissViewControllerAnimated:YES completion:^{}];
    
    image = [Tools imageWithImageSimple:image scaledToSize:CGSizeMake(100, 100)];
    
    [self upImg:image];
}



- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController {
    
    [cropperViewController dismissViewControllerAnimated:YES completion:^{}];
}


@end
